#!/bin/sh

if [ `yum list installed | grep docker | wc -l` > 0 ]
then
  echo "docker is installed"
else
  install_docker
fi

function install_docker() {
    echo "Step 1: install docker"
    curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

    echo "Step 2: add no root user to docker"
    sudo groupadd docker
    sudo usermod -aG docker `whoami`

    echo "Step 3: change docker config"
    docker_etc='/etc/docker'
    [ -e $docker_etc ] || sudo mkdir $docker_etc
    docker_daemon_json="$docker_etc/daemon.json"
    sudo bash -c "cat > $docker_daemon_json" <<EOF
{
    "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

    echo "Step 3: reload docker daemon"
    sudo systemctl daemon-reload
    sudo systemctl enable docker
    sudo systemctl start docker
    sudo docker info
}

